{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bd6be7c5",
   "metadata": {},
   "source": [
    "# Lollipop Plot\n",
    "\n",
    "A lollipop plot displays each element of a dataset as a segment and a circle. It is usually combined with the `count` stat, and is especially useful when you have several bars of the same height."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5f3557a",
   "metadata": {},
   "source": [
    "1. [Parameters `size`, `stroke` and `linewidth`](#stroke)\n",
    "\n",
    "2. [Parameter `fatten`](#fatten)\n",
    "\n",
    "3. [Horizontal Sticks](#direction)\n",
    "\n",
    "4. [Sloped Baseline](#slope)\n",
    "\n",
    "5. [Parameter `stat`](#stat)\n",
    "\n",
    "6. [Lollipops in Marginal Layer](#ggmarginal)\n",
    "\n",
    "7. [Lollipops and a Regression Line](#slope_and_intercept)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ba071491",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"lFzwm3\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v3.2.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"lFzwm3\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"lFzwm3\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "06477cad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Lets-Plot Kotlin API v.4.4.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.3.2.0."
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LetsPlot.getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "66de6718",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "            <div id=\"kotlin_out_0\"/>\n",
       "            <script type=\"text/javascript\">\n",
       "                            if(!window.kotlinQueues) {\n",
       "                window.kotlinQueues = {};\n",
       "            }\n",
       "            if(!window.kotlinQueues[\"DataFrame\"]) {\n",
       "                var resQueue = [];\n",
       "                window.kotlinQueues[\"DataFrame\"] = resQueue;\n",
       "                window[\"call_DataFrame\"] = function(f) {\n",
       "                    resQueue.push(f);\n",
       "                }\n",
       "            }\n",
       "            (function (){\n",
       "                var modifiers = [(function(script) {\n",
       "    script.src = \"https://cdn.jsdelivr.net/gh/Kotlin/dataframe@3db46ccccaa1291c0627307d64133317f545e6ae/core/src/main/resources/init.js\"\n",
       "    script.type = \"text/javascript\";\n",
       "})];\n",
       "                var e = document.getElementById(\"kotlin_out_0\");\n",
       "                modifiers.forEach(function (gen) {\n",
       "                    var script = document.createElement(\"script\");\n",
       "                    gen(script)\n",
       "                    script.addEventListener(\"load\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {f();};\n",
       "                        window.kotlinQueues[\"DataFrame\"].forEach(function(f) {f();});\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                    }, false);\n",
       "                    script.addEventListener(\"error\", function() {\n",
       "                        window[\"call_DataFrame\"] = function(f) {};\n",
       "                        window.kotlinQueues[\"DataFrame\"] = [];\n",
       "                        var div = document.createElement(\"div\");\n",
       "                        div.style.color = 'darkred';\n",
       "                        div.textContent = 'Error loading resource DataFrame';\n",
       "                        document.getElementById(\"kotlin_out_0\").appendChild(div);\n",
       "                    }, false);\n",
       "                    \n",
       "                    e.appendChild(script);\n",
       "                });\n",
       "            })();\n",
       "            </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "                <style>\n",
       "                :root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       "                </style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%use dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d526d855",
   "metadata": {},
   "outputs": [],
   "source": [
    "import jetbrains.datalore.plot.base.stat.regression.LinearRegression\n",
    "import kotlin.random.Random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "166e457f",
   "metadata": {},
   "outputs": [],
   "source": [
    "val rnd = Random(0)\n",
    "val data = mapOf(\n",
    "    \"x\" to (-15..14).toList(),\n",
    "    \"y\" to (0..29).map { rnd.nextDouble(1.0, 5.0) },\n",
    "    \"sugar\" to (150..179).toList()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "75bf0748",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"BMYJPV\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":600.0,\n",
       "\"height\":200.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"BMYJPV\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letsPlot(data) { x = \"x\"; y = \"y\" } + geomLollipop() + ggsize(600, 200)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a401867",
   "metadata": {},
   "source": [
    "<a id=\"stroke\"></a>\n",
    "\n",
    "#### 1. Parameters `size`, `stroke` and `linewidth`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3007532a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"UY2LYU\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":1,\n",
       "\"nrow\":3,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"variable 'size'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"size\":\"sugar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365],\n",
       "\"sugar\":[150.0,151.0,152.0,153.0,154.0,155.0,156.0,157.0,158.0,159.0,160.0,161.0,162.0,163.0,164.0,165.0,166.0,167.0,168.0,169.0,170.0,171.0,172.0,173.0,174.0,175.0,176.0,177.0,178.0,179.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"variable 'size' and 'stroke'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"size\":\"sugar\",\n",
       "\"stroke\":\"sugar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365],\n",
       "\"sugar\":[150.0,151.0,152.0,153.0,154.0,155.0,156.0,157.0,158.0,159.0,160.0,161.0,162.0,163.0,164.0,165.0,166.0,167.0,168.0,169.0,170.0,171.0,172.0,173.0,174.0,175.0,176.0,177.0,178.0,179.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"variable 'size' and 'linewidth'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\",\n",
       "\"size\":\"sugar\",\n",
       "\"linewidth\":\"sugar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365],\n",
       "\"sugar\":[150.0,151.0,152.0,153.0,154.0,155.0,156.0,157.0,158.0,159.0,160.0,161.0,162.0,163.0,164.0,165.0,166.0,167.0,168.0,169.0,170.0,171.0,172.0,173.0,174.0,175.0,176.0,177.0,178.0,179.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"ggsize\":{\n",
       "\"width\":800.0,\n",
       "\"height\":800.0\n",
       "},\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"UY2LYU\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val plots = listOf(\n",
    "    letsPlot(data) { x = \"x\"; y = \"y\"; size = \"sugar\" } + \n",
    "        geomLollipop() + ggtitle(\"variable 'size'\"),\n",
    "    letsPlot(data) { x = \"x\"; y = \"y\"; size = \"sugar\"; stroke = \"sugar\" } + \n",
    "        geomLollipop() + ggtitle(\"variable 'size' and 'stroke'\"),\n",
    "    letsPlot(data) { x = \"x\"; y = \"y\"; size = \"sugar\"; linewidth = \"sugar\" } + \n",
    "        geomLollipop() + ggtitle(\"variable 'size' and 'linewidth'\")\n",
    ")\n",
    "    \n",
    "gggrid(plots, ncol=1) + ggsize(800, 800)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5d6d73f",
   "metadata": {},
   "source": [
    "<a id=\"fatten\"></a>\n",
    "\n",
    "#### 2. Parameter `fatten`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e1de6c92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"kCTQ9A\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":2,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"fatten=2.5 (default)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"fatten=5\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"fatten\":5.0,\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"kCTQ9A\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(\n",
    "    listOf(\n",
    "        letsPlot(data) { x = \"x\"; y = \"y\"} + geomLollipop() + ggtitle(\"fatten=2.5 (default)\"),\n",
    "        letsPlot(data) { x = \"x\"; y = \"y\"} + geomLollipop(fatten = 5) + ggtitle(\"fatten=5\"),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89b3fbaa",
   "metadata": {},
   "source": [
    "<a id=\"direction\"></a>\n",
    "\n",
    "#### 3. Horizontal Sticks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f1efe926",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"BySvmG\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"y\",\n",
       "\"y\":\"x\"\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"dir\":\"h\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"BySvmG\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letsPlot(data) { x = \"y\"; y = \"x\" } + geomLollipop(dir = \"h\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5fcee23",
   "metadata": {},
   "source": [
    "<a id=\"slope\"></a>\n",
    "\n",
    "#### 4. Sloped Baseline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1fb4fb53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"BGZ8Ws\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":3,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"dir='v' (default)\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"fixed\",\n",
       "\"flip\":false,\n",
       "\"ylim\":[-12.0,12.0]\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"black\",\n",
       "\"size\":1.5,\n",
       "\"intercept\":1.0,\n",
       "\"linetype\":\"dotted\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"abline\",\n",
       "\"slope\":0.5,\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":21.0,\n",
       "\"intercept\":1.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"slope\":0.5,\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"dir='h'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"fixed\",\n",
       "\"flip\":false,\n",
       "\"ylim\":[-12.0,12.0]\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"black\",\n",
       "\"size\":1.5,\n",
       "\"intercept\":1.0,\n",
       "\"linetype\":\"dotted\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"abline\",\n",
       "\"slope\":0.5,\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":21.0,\n",
       "\"intercept\":1.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"slope\":0.5,\n",
       "\"dir\":\"h\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"dir='s'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"x\",\n",
       "\"y\":\"y\"\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"fixed\",\n",
       "\"flip\":false,\n",
       "\"ylim\":[-12.0,12.0]\n",
       "},\n",
       "\"data\":{\n",
       "\"x\":[-15.0,-14.0,-13.0,-12.0,-11.0,-10.0,-9.0,-8.0,-7.0,-6.0,-5.0,-4.0,-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0],\n",
       "\"y\":[3.19853260088592,4.395421902021933,3.6364119164177136,1.0319517367469504,2.2998763837291856,1.0404452718973043,3.138301565296638,3.7667147138927213,4.793515744708824,1.006418562654987,4.58535227559738,2.806451926753802,2.9276762915481616,2.24689234256625,1.7296184490395174,2.4209612140648678,1.398406091081863,2.8634679302809154,4.154731306748639,1.6718255352928337,3.122243510997544,2.129627674692007,3.0773937871244703,2.027725709441059,4.482308804863544,4.223833749518779,1.4725130572499223,1.2490908406745307,3.500620271764213,2.644419081624365]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"black\",\n",
       "\"size\":1.5,\n",
       "\"intercept\":1.0,\n",
       "\"linetype\":\"dotted\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"abline\",\n",
       "\"slope\":0.5,\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":21.0,\n",
       "\"intercept\":1.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"slope\":0.5,\n",
       "\"dir\":\"s\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"BGZ8Ws\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val slope = 0.5\n",
    "val intercept = 1\n",
    "\n",
    "val abline = letsPlot(data) { x = \"x\"; y = \"y\" } +\n",
    "    geomABLine(intercept = intercept, slope = slope, color = \"black\", linetype = \"dotted\", size = 1.5) + \n",
    "    coordFixed(ylim = -12 to 12)\n",
    "    \n",
    "gggrid(\n",
    "    listOf(\n",
    "        abline + geomLollipop(intercept = intercept, slope = slope, shape = 21) + ggtitle(\"dir='v' (default)\"),\n",
    "        abline + geomLollipop(intercept = intercept, slope = slope, shape = 21, dir = \"h\") + ggtitle(\"dir='h'\"),\n",
    "        abline + geomLollipop(intercept = intercept, slope = slope, shape = 21, dir = \"s\") + ggtitle(\"dir='s'\"),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36f3fc38",
   "metadata": {},
   "source": [
    "<a id=\"stat\"></a>\n",
    "\n",
    "#### 5. Parameter `stat`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6f895fd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"}]}",
      "text/html": [
       "        <html>\n",
       "        <head>\n",
       "            <style type=\"text/css\">\n",
       "                :root {\n",
       "    --background: #fff;\n",
       "    --background-odd: #f5f5f5;\n",
       "    --background-hover: #d9edfd;\n",
       "    --header-text-color: #474747;\n",
       "    --text-color: #848484;\n",
       "    --text-color-dark: #000;\n",
       "    --text-color-medium: #737373;\n",
       "    --text-color-pale: #b3b3b3;\n",
       "    --inner-border-color: #aaa;\n",
       "    --bold-border-color: #000;\n",
       "    --link-color: #296eaa;\n",
       "    --link-color-pale: #296eaa;\n",
       "    --link-hover: #1a466c;\n",
       "}\n",
       "\n",
       ":root[theme=\"dark\"], :root [data-jp-theme-light=\"false\"], .dataframe_dark{\n",
       "    --background: #303030;\n",
       "    --background-odd: #3c3c3c;\n",
       "    --background-hover: #464646;\n",
       "    --header-text-color: #dddddd;\n",
       "    --text-color: #b3b3b3;\n",
       "    --text-color-dark: #dddddd;\n",
       "    --text-color-medium: #b2b2b2;\n",
       "    --text-color-pale: #737373;\n",
       "    --inner-border-color: #707070;\n",
       "    --bold-border-color: #777777;\n",
       "    --link-color: #008dc0;\n",
       "    --link-color-pale: #97e1fb;\n",
       "    --link-hover: #00688e;\n",
       "}\n",
       "\n",
       "p.dataframe_description {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe {\n",
       "    font-family: \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n",
       "    font-size: 12px;\n",
       "    background-color: var(--background);\n",
       "    color: var(--text-color-dark);\n",
       "    border: none;\n",
       "    border-collapse: collapse;\n",
       "}\n",
       "\n",
       "table.dataframe th, td {\n",
       "    padding: 6px;\n",
       "    border: 1px solid transparent;\n",
       "    text-align: left;\n",
       "}\n",
       "\n",
       "table.dataframe th {\n",
       "    background-color: var(--background);\n",
       "    color: var(--header-text-color);\n",
       "}\n",
       "\n",
       "table.dataframe td {\n",
       "    vertical-align: top;\n",
       "}\n",
       "\n",
       "table.dataframe th.bottomBorder {\n",
       "    border-bottom-color: var(--bold-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(odd) {\n",
       "    background: var(--background-odd);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:nth-child(even) {\n",
       "    background: var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe tbody > tr:hover {\n",
       "    background: var(--background-hover);\n",
       "}\n",
       "\n",
       "table.dataframe a {\n",
       "    cursor: pointer;\n",
       "    color: var(--link-color);\n",
       "    text-decoration: none;\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover > td a {\n",
       "    color: var(--link-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe a:hover {\n",
       "    color: var(--link-hover);\n",
       "    text-decoration: underline;\n",
       "}\n",
       "\n",
       "table.dataframe img {\n",
       "    max-width: fit-content;\n",
       "}\n",
       "\n",
       "table.dataframe th.complex {\n",
       "    background-color: var(--background);\n",
       "    border: 1px solid var(--background);\n",
       "}\n",
       "\n",
       "table.dataframe .leftBorder {\n",
       "    border-left-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightBorder {\n",
       "    border-right-color: var(--inner-border-color);\n",
       "}\n",
       "\n",
       "table.dataframe .rightAlign {\n",
       "    text-align: right;\n",
       "}\n",
       "\n",
       "table.dataframe .expanderSvg {\n",
       "    width: 8px;\n",
       "    height: 8px;\n",
       "    margin-right: 3px;\n",
       "}\n",
       "\n",
       "table.dataframe .expander {\n",
       "    display: flex;\n",
       "    align-items: center;\n",
       "}\n",
       "\n",
       "/* formatting */\n",
       "\n",
       "table.dataframe .null {\n",
       "    color: var(--text-color-pale);\n",
       "}\n",
       "\n",
       "table.dataframe .structural {\n",
       "    color: var(--text-color-medium);\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .dataFrameCaption {\n",
       "    font-weight: bold;\n",
       "}\n",
       "\n",
       "table.dataframe .numbers {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe td:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "table.dataframe tr:hover .formatted .structural, .null {\n",
       "    color: var(--text-color-dark);\n",
       "}\n",
       "\n",
       "\n",
       "\n",
       "\n",
       "            </style>\n",
       "        </head>\n",
       "        <body>\n",
       "            \n",
       "<table class=\"dataframe\" id=\"df_1308622848\"></table>\n",
       "\n",
       "<p class=\"dataframe_description\">DataFrame: rowsCount = 3, columnsCount = 12</p>\n",
       "        </body>\n",
       "        <script>\n",
       "            \n",
       "/*<!--*/\n",
       "call_DataFrame(function() { DataFrame.addTable({ cols: [{ name: \"<span title=\\\"untitled: Int\\\">untitled</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">3</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"manufacturer: String\\\">manufacturer</span>\", children: [], rightAlign: false, values: [\"audi\",\"audi\",\"audi\"] }, \n",
       "{ name: \"<span title=\\\"model: String\\\">model</span>\", children: [], rightAlign: false, values: [\"a4\",\"a4\",\"a4\"] }, \n",
       "{ name: \"<span title=\\\"displ: Double\\\">displ</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1.8</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1.8</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2.0</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"year: Int\\\">year</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1999</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">1999</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">2008</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"cyl: Int\\\">cyl</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">4</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"trans: String\\\">trans</span>\", children: [], rightAlign: false, values: [\"auto(l5)\",\"manual(m5)\",\"manual(m6)\"] }, \n",
       "{ name: \"<span title=\\\"drv: String\\\">drv</span>\", children: [], rightAlign: false, values: [\"f\",\"f\",\"f\"] }, \n",
       "{ name: \"<span title=\\\"cty: Int\\\">cty</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">18</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">21</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">20</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"hwy: Int\\\">hwy</span>\", children: [], rightAlign: true, values: [\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">29</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">29</span></span>\",\"<span class=\\\"formatted\\\" title=\\\"\\\"><span class=\\\"numbers\\\">31</span></span>\"] }, \n",
       "{ name: \"<span title=\\\"fl: String\\\">fl</span>\", children: [], rightAlign: false, values: [\"p\",\"p\",\"p\"] }, \n",
       "{ name: \"<span title=\\\"class: String\\\">class</span>\", children: [], rightAlign: false, values: [\"compact\",\"compact\",\"compact\"] }, \n",
       "], id: 1308622848, rootId: 1308622848, totalRows: 3 } ) });\n",
       "/*-->*/\n",
       "\n",
       "call_DataFrame(function() { DataFrame.renderTable(1308622848) });\n",
       "\n",
       "\n",
       "        </script>\n",
       "        </html>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n",
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "66fac357",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"zfEgyH\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"layout\":{\n",
       "\"name\":\"grid\",\n",
       "\"ncol\":3,\n",
       "\"nrow\":1,\n",
       "\"fit\":true,\n",
       "\"align\":false\n",
       "},\n",
       "\"figures\":[{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"stat='count'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"class\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "\"..count..\":[47.0,41.0,62.0,5.0,11.0,33.0,35.0],\n",
       "\"class\":[\"compact\",\"midsize\",\"suv\",\"2seater\",\"minivan\",\"pickup\",\"subcompact\"]\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"stat='bin'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"hwy\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "\"..count..\":[5.0,0.0,2.0,10.0,7.0,31.0,23.0,11.0,2.0,7.0,7.0,13.0,15.0,32.0,14.0,29.0,4.0,7.0,4.0,2.0,1.0,2.0,2.0,1.0,0.0,0.0,1.0,0.0,0.0,2.0],\n",
       "\"hwy\":[11.811555555555556,12.928,14.044444444444444,15.160888888888888,16.27733333333333,17.39377777777778,18.51022222222222,19.626666666666665,20.743111111111112,21.859555555555552,22.976,24.092444444444446,25.208888888888886,26.325333333333333,27.441777777777776,28.55822222222222,29.674666666666667,30.791111111111107,31.907555555555554,33.024,34.14044444444444,35.25688888888889,36.373333333333335,37.489777777777775,38.60622222222222,39.72266666666666,40.83911111111111,41.955555555555556,43.071999999999996,44.18844444444444]\n",
       "}\n",
       "}]\n",
       "},{\n",
       "\"ggtitle\":{\n",
       "\"text\":\"stat='density'\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"hwy\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"density\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"n\":30.0,\n",
       "\"data\":{\n",
       "\"hwy\":[12.0,13.10344827586207,14.206896551724139,15.310344827586206,16.413793103448278,17.517241379310345,17.517241379310345,18.620689655172413,19.724137931034484,20.82758620689655,21.93103448275862,23.03448275862069,23.03448275862069,24.137931034482758,25.241379310344826,26.344827586206897,26.344827586206897,27.448275862068964,28.551724137931036,29.655172413793103,30.75862068965517,31.862068965517242,32.96551724137931,34.06896551724138,35.172413793103445,36.275862068965516,37.37931034482759,38.48275862068965,39.58620689655172,40.689655172413794,41.79310344827586,42.89655172413793,44.0],\n",
       "\"..density..\":[0.009329787573445901,0.015942646047671205,0.026976923010417896,0.04170560119548454,0.054695748060277875,0.05937475132811441,0.05937475132811441,0.05441352871691398,0.044783132603310266,0.03728781666722159,0.036836353291363054,0.044730761346244624,0.044730761346244624,0.05779048283974624,0.0690277842079404,0.07193668615051252,0.07193668615051252,0.0659962783184005,0.05544982245581327,0.043539705313257474,0.03171503227047856,0.021532315226313208,0.014183334526420193,0.009723937126770476,0.0072131179937258115,0.005368034189768139,0.003572903327211723,0.0021344450506854454,0.001467552545046875,0.001474099395236774,0.0017924430938290846,0.0021211603215793833,0.002134285708001272],\n",
       "\"..quantile..\":[0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.5,0.5,0.75,0.75,0.75,0.75,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]\n",
       "}\n",
       "}]\n",
       "}],\n",
       "\"kind\":\"subplots\"\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"zfEgyH\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gggrid(\n",
    "    listOf(\n",
    "        letsPlot(df.toMap()) { x = \"class\" } + geomLollipop(stat = Stat.count()) + ggtitle(\"stat='count'\"),\n",
    "        letsPlot(df.toMap()) { x = \"hwy\" } + geomLollipop(stat = Stat.bin()) + ggtitle(\"stat='bin'\"),\n",
    "        letsPlot(df.toMap()) { x = \"hwy\" } + geomLollipop(stat = Stat.density(n=30)) + ggtitle(\"stat='density'\")\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa9cd80d",
   "metadata": {},
   "source": [
    "<a id=\"ggmarginal\"></a>\n",
    "\n",
    "#### 6. Lollipops in Marginal Layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d17028b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"9dMFJS\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"hwy\",\n",
       "\"y\":\"cty\"\n",
       "},\n",
       "\"data\":{\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"bin2d\",\n",
       "\"position\":\"identity\",\n",
       "\"binwidth\":[1.0,1.0],\n",
       "\"geom\":\"tile\",\n",
       "\"data\":{\n",
       "\"..count..\":[5.0,2.0,10.0,3.0,2.0,2.0,5.0,1.0,12.0,10.0,3.0,5.0,3.0,1.0,1.0,4.0,4.0,5.0,4.0,3.0,4.0,2.0,3.0,2.0,2.0,2.0,4.0,1.0,2.0,2.0,7.0,2.0,3.0,2.0,3.0,2.0,4.0,1.0,5.0,3.0,14.0,8.0,1.0,1.0,1.0,3.0,4.0,4.0,2.0,1.0,3.0,3.0,3.0,1.0,1.0,14.0,2.0,1.0,2.0,1.0,1.0,1.0,4.0,1.0,1.0,1.0,2.0,1.0,1.0,1.0,1.0,2.0,1.0,1.0,1.0,1.0,1.0,1.0],\n",
       "\"cty\":[9.0,11.0,11.0,11.0,12.0,13.0,11.0,12.0,13.0,14.0,15.0,12.0,13.0,14.0,15.0,13.0,14.0,15.0,14.0,15.0,16.0,15.0,15.0,16.0,17.0,15.0,16.0,18.0,15.0,16.0,17.0,18.0,15.0,16.0,17.0,18.0,19.0,20.0,16.0,17.0,18.0,19.0,20.0,21.0,17.0,18.0,19.0,20.0,21.0,18.0,19.0,20.0,18.0,19.0,20.0,21.0,22.0,23.0,21.0,22.0,24.0,20.0,21.0,22.0,23.0,23.0,24.0,25.0,24.0,28.0,26.0,26.0,24.0,25.0,28.0,29.0,33.0,35.0],\n",
       "\"hwy\":[12.0,14.0,15.0,16.0,16.0,16.0,17.0,17.0,17.0,17.0,17.0,18.0,18.0,18.0,18.0,19.0,19.0,19.0,20.0,20.0,20.0,21.0,22.0,22.0,22.0,23.0,23.0,23.0,24.0,24.0,24.0,24.0,25.0,25.0,25.0,25.0,25.0,25.0,26.0,26.0,26.0,26.0,26.0,26.0,27.0,27.0,27.0,27.0,27.0,28.0,28.0,28.0,29.0,29.0,29.0,29.0,29.0,29.0,30.0,30.0,30.0,31.0,31.0,31.0,31.0,32.0,32.0,32.0,33.0,33.0,34.0,35.0,36.0,36.0,37.0,41.0,44.0,44.0]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"color\":\"..count..\"\n",
       "},\n",
       "\"stat\":\"count\",\n",
       "\"orientation\":\"y\",\n",
       "\"margin_side\":\"r\",\n",
       "\"size\":1.0,\n",
       "\"marginal\":true,\n",
       "\"margin_size\":0.2,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"data\":{\n",
       "\"..count..\":[26.0,23.0,11.0,19.0,20.0,24.0,16.0,19.0,20.0,21.0,8.0,4.0,5.0,2.0,5.0,2.0,3.0,3.0,1.0,1.0,1.0],\n",
       "\"cty\":[18.0,21.0,20.0,16.0,19.0,15.0,17.0,14.0,11.0,13.0,12.0,22.0,9.0,28.0,24.0,25.0,23.0,26.0,33.0,35.0,29.0],\n",
       "\"hwy\":[26.0,23.0,11.0,19.0,20.0,24.0,16.0,19.0,20.0,21.0,8.0,4.0,5.0,2.0,5.0,2.0,3.0,3.0,1.0,1.0,1.0]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"9dMFJS\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letsPlot(df.toMap()) { x = \"hwy\"; y = \"cty\" } +\n",
    "    geomBin2D(binWidth = 1 to 1) +\n",
    "    ggmarginal(\"r\", size=.2,\n",
    "               layer = geomLollipop(stat = Stat.count(), orientation = \"y\", size = 1) { color = \"..count..\" } )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f91b9f7",
   "metadata": {},
   "source": [
    "<a id=\"slope_and_intercept\"></a>\n",
    "\n",
    "#### 7. Lollipops and a Regression Line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5ce3b9ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"fuMr4j\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"hwy\",\n",
       "\"y\":\"cty\"\n",
       "},\n",
       "\"coord\":{\n",
       "\"name\":\"fixed\",\n",
       "\"flip\":false\n",
       "},\n",
       "\"data\":{\n",
       "\"cty\":[18.0,21.0,20.0,21.0,16.0,18.0,18.0,18.0,16.0,20.0,19.0,15.0,17.0,17.0,15.0,15.0,17.0,16.0,14.0,11.0,14.0,13.0,12.0,16.0,15.0,16.0,15.0,15.0,14.0,11.0,11.0,14.0,19.0,22.0,18.0,18.0,17.0,18.0,17.0,16.0,16.0,17.0,17.0,11.0,15.0,15.0,16.0,16.0,15.0,14.0,13.0,14.0,14.0,14.0,9.0,11.0,11.0,13.0,13.0,9.0,13.0,11.0,13.0,11.0,12.0,9.0,13.0,13.0,12.0,9.0,11.0,11.0,13.0,11.0,11.0,11.0,12.0,14.0,15.0,14.0,13.0,13.0,13.0,14.0,14.0,13.0,13.0,13.0,11.0,13.0,18.0,18.0,17.0,16.0,15.0,15.0,15.0,15.0,14.0,28.0,24.0,25.0,23.0,24.0,26.0,25.0,24.0,21.0,18.0,18.0,21.0,21.0,18.0,18.0,19.0,19.0,19.0,20.0,20.0,17.0,16.0,17.0,17.0,15.0,15.0,14.0,9.0,14.0,13.0,11.0,11.0,12.0,12.0,11.0,11.0,11.0,12.0,14.0,13.0,13.0,13.0,21.0,19.0,23.0,23.0,19.0,19.0,18.0,19.0,19.0,14.0,15.0,14.0,12.0,18.0,16.0,17.0,18.0,16.0,18.0,18.0,20.0,19.0,20.0,18.0,21.0,19.0,19.0,19.0,20.0,20.0,19.0,20.0,15.0,16.0,15.0,15.0,16.0,14.0,21.0,21.0,21.0,21.0,18.0,18.0,19.0,21.0,21.0,21.0,22.0,18.0,18.0,18.0,24.0,24.0,26.0,28.0,26.0,11.0,13.0,15.0,16.0,17.0,15.0,15.0,15.0,16.0,21.0,19.0,21.0,22.0,17.0,33.0,21.0,19.0,22.0,21.0,21.0,21.0,16.0,17.0,35.0,29.0,21.0,19.0,20.0,20.0,21.0,18.0,19.0,21.0,16.0,18.0,17.0],\n",
       "\"hwy\":[29.0,29.0,31.0,30.0,26.0,26.0,27.0,26.0,25.0,28.0,27.0,25.0,25.0,25.0,25.0,24.0,25.0,23.0,20.0,15.0,20.0,17.0,17.0,26.0,23.0,26.0,25.0,24.0,19.0,14.0,15.0,17.0,27.0,30.0,26.0,29.0,26.0,24.0,24.0,22.0,22.0,24.0,24.0,17.0,22.0,21.0,23.0,23.0,19.0,18.0,17.0,17.0,19.0,19.0,12.0,17.0,15.0,17.0,17.0,12.0,17.0,16.0,18.0,15.0,16.0,12.0,17.0,17.0,16.0,12.0,15.0,16.0,17.0,15.0,17.0,17.0,18.0,17.0,19.0,17.0,19.0,19.0,17.0,17.0,17.0,16.0,16.0,17.0,15.0,17.0,26.0,25.0,26.0,24.0,21.0,22.0,23.0,22.0,20.0,33.0,32.0,32.0,29.0,32.0,34.0,36.0,36.0,29.0,26.0,27.0,30.0,31.0,26.0,26.0,28.0,26.0,29.0,28.0,27.0,24.0,24.0,24.0,22.0,19.0,20.0,17.0,12.0,19.0,18.0,14.0,15.0,18.0,18.0,15.0,17.0,16.0,18.0,17.0,19.0,19.0,17.0,29.0,27.0,31.0,32.0,27.0,26.0,26.0,25.0,25.0,17.0,17.0,20.0,18.0,26.0,26.0,27.0,28.0,25.0,25.0,24.0,27.0,25.0,26.0,23.0,26.0,26.0,26.0,26.0,25.0,27.0,25.0,27.0,20.0,20.0,19.0,17.0,20.0,17.0,29.0,27.0,31.0,31.0,26.0,26.0,28.0,27.0,29.0,31.0,31.0,26.0,26.0,27.0,30.0,33.0,35.0,37.0,35.0,15.0,18.0,20.0,20.0,22.0,17.0,19.0,18.0,20.0,29.0,26.0,29.0,29.0,24.0,44.0,29.0,26.0,29.0,29.0,29.0,29.0,23.0,24.0,44.0,41.0,29.0,26.0,28.0,29.0,29.0,29.0,28.0,29.0,26.0,26.0,26.0]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"smooth\",\n",
       "\"level\":0.99,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"smooth\",\n",
       "\"data\":{\n",
       "\"..ymax..\":[9.50415886582937,9.768091600706402,10.032129715616245,10.296282704237429,10.560561133322803,10.824976780291285,11.089542788414695,11.3542738411179,11.619186356618863,11.884298703578818,12.149631437490664,12.415207556046122,12.681052769483937,12.947195778684792,13.213668549265494,13.48050656386581,13.747749027032953,14.015438987630432,14.283623333007542,14.552352598470543,14.821680527165485,15.091663312895,15.362358466464533,15.63382327020828,15.906112829469526,16.179277794096045,16.45336190046701,16.728399559399502,17.004413764797413,17.281414599367075,17.55939855463104,17.83834876926317,18.118236148925256,18.39902119984308,18.68065632083691,18.963088271093703,19.24626055959734,19.530115567718273,19.81459629514555,20.099647691437045,20.385217589743476,20.671257292729493,20.957721875910963,21.24457027555882,21.531765222196697,21.819273070763476,22.10706356761021,22.395109584336687,22.683386839837112,22.971873625038707,23.260550539583104,23.54940024587925,23.838407243266698,24.127557663203977,24.416839085212672,24.70624037258109,24.99575152642426,25.285363556505054,25.575068367171298,25.86485865680163,26.154727829243857,26.44466991584781,26.73467950682481,27.024751690797444,27.31488200152964,27.605066370945526,27.89530108765322,28.185582760287133,28.475908285068883,28.76627481706345,29.056679744674497,29.347120666981453,29.637595373572402,29.928101826571286,30.218638144596774,30.50920258842367,30.799793548147022,31.090409531674247,31.381049154392617,31.671711129878435],\n",
       "\"cty\":[9.042830932083042,9.319577914018055,9.596324895953067,9.873071877888078,10.14981885982309,10.426565841758103,10.703312823693116,10.980059805628128,11.25680678756314,11.533553769498152,11.810300751433164,12.087047733368175,12.363794715303188,12.640541697238199,12.917288679173213,13.194035661108224,13.470782643043234,13.747529624978249,14.02427660691326,14.30102358884827,14.577770570783285,14.854517552718296,15.131264534653306,15.408011516588319,15.684758498523333,15.961505480458344,16.238252462393355,16.514999444328367,16.79174642626338,17.068493408198393,17.345240390133405,17.621987372068414,17.898734354003427,18.17548133593844,18.452228317873455,18.72897529980846,19.005722281743477,19.28246926367849,19.559216245613502,19.83596322754851,20.112710209483524,20.389457191418536,20.66620417335355,20.94295115528856,21.21969813722357,21.496445119158587,21.773192101093596,22.049939083028608,22.32668606496362,22.603433046898633,22.880180028833646,23.156927010768655,23.433673992703667,23.71042097463868,23.987167956573696,24.2639149385087,24.540661920443718,24.817408902378727,25.09415588431374,25.370902866248752,25.647649848183764,25.92439683011878,26.20114381205379,26.477890793988802,26.75463777592381,27.031384757858824,27.308131739793836,27.584878721728852,27.861625703663865,28.138372685598874,28.415119667533883,28.691866649468896,28.968613631403908,29.24536061333892,29.522107595273937,29.798854577208946,30.07560155914396,30.352348541078968,30.62909552301398,30.905842504948993],\n",
       "\"..ymin..\":[8.581502998336715,8.871064227329708,9.16052007628989,9.449861051538727,9.739076586323378,10.028154903224921,10.317082858971537,10.605845770138357,10.894427218507415,11.182808835417486,11.470970065375665,11.758887910690229,12.046536661122438,12.333887615791605,12.620908809080932,12.907564758350638,13.193816259053516,13.479620262326065,13.764929880818977,14.049694579225998,14.333860614401084,14.617371792541592,14.90017060284208,15.182199762968358,15.46340416757714,15.743733166820643,16.0231430243197,16.301599329257233,16.579079087729347,16.85557221702971,17.13108222563577,17.405625974873658,17.679232559081598,17.9519414720338,18.22380031491,18.49486232852322,18.765184003889615,19.034822959638706,19.303836196081456,19.572278763659977,19.84020282922357,20.10765709010758,20.374686470796135,20.6413320350183,20.907631052250444,21.173617167553697,21.43932063457698,21.70476858172053,21.96998529009013,22.23499246875856,22.499809518084188,22.76445377565806,23.028940742140637,23.293284286073384,23.55749682793472,23.821589504436314,24.085572314463175,24.3494542482524,24.61324340145618,24.876947075695874,25.140571867123672,25.40412374438975,25.667608117282768,25.93102989718016,26.194393550317983,26.457703144772122,26.720962391934453,26.984174683170572,27.247343122258847,27.510470554134297,27.77355959039327,28.036612631956338,28.299631889235414,28.562619400106556,28.8255770459511,29.088506565994223,29.351409570140895,29.614287550483688,29.877141891635343,30.13997388001955],\n",
       "\"hwy\":[12.0,12.405063291139241,12.810126582278482,13.215189873417721,13.620253164556962,14.025316455696203,14.430379746835444,14.835443037974684,15.240506329113924,15.645569620253164,16.050632911392405,16.455696202531644,16.860759493670887,17.265822784810126,17.67088607594937,18.075949367088608,18.481012658227847,18.88607594936709,19.29113924050633,19.696202531645568,20.10126582278481,20.50632911392405,20.91139240506329,21.31645569620253,21.721518987341774,22.126582278481013,22.531645569620252,22.936708860759495,23.341772151898734,23.746835443037973,24.151898734177216,24.556962025316455,24.962025316455694,25.367088607594937,25.77215189873418,26.177215189873415,26.582278481012658,26.9873417721519,27.39240506329114,27.79746835443038,28.20253164556962,28.60759493670886,29.0126582278481,29.417721518987342,29.82278481012658,30.227848101265824,30.632911392405063,31.037974683544302,31.443037974683545,31.848101265822784,32.25316455696203,32.65822784810126,33.063291139240505,33.46835443037975,33.87341772151899,34.278481012658226,34.68354430379747,35.088607594936704,35.49367088607595,35.89873417721519,36.30379746835443,36.708860759493675,37.11392405063291,37.51898734177215,37.92405063291139,38.32911392405063,38.734177215189874,39.139240506329116,39.54430379746836,39.949367088607595,40.35443037974683,40.75949367088607,41.164556962025316,41.56962025316456,41.9746835443038,42.379746835443036,42.78481012658228,43.189873417721515,43.59493670886076,44.0]\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":1.2,\n",
       "\"color\":\"black\",\n",
       "\"shape\":21.0,\n",
       "\"intercept\":0.8442015922583117,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"lollipop\",\n",
       "\"fill\":\"magenta\",\n",
       "\"slope\":0.683219111652061,\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"fuMr4j\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val xs = df[\"hwy\"].toList().map { (it as Number).toDouble() }\n",
    "val ys = df[\"cty\"].toList().map { (it as Number).toDouble() }\n",
    "\n",
    "fun linearModel(x: Double) = LinearRegression(xs, ys, 0.95).evalX(x).y\n",
    "\n",
    "val intercept = linearModel(0.0)\n",
    "val slope = linearModel(1.0) - intercept\n",
    "\n",
    "letsPlot(df.toMap()) { x = \"hwy\"; y = \"cty\" } +\n",
    "    geomSmooth(level = 0.99) + \n",
    "    geomLollipop(slope = slope, intercept = intercept,\n",
    "                 size = 1.2, shape = 21, color = \"black\", fill = \"magenta\") +\n",
    "    coordFixed()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "codemirror_mode": "text/x-kotlin",
   "file_extension": ".kt",
   "mimetype": "text/x-kotlin",
   "name": "kotlin",
   "nbconvert_exporter": "",
   "pygments_lexer": "kotlin",
   "version": "1.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
